package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.action.ActionModel;
import com.park.client.service.action.ActionService;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/action")
public class AdminActionController extends BaseController{
	
	@Autowired
	private ActionService actionService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<ActionModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<ActionModel> pageData = new PageData<>();
		ActionModel action = new ActionModel();
		Integer total = actionService.selectTotal(action);
		Page page = new Page(pageNow,total,rows);
		action.setStartPos(page.getStartPos());
		action.setPageSize(page.getPageSize());
		List<ActionModel> list = actionService.selectPagination(action);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("actionList")
	public String actionList(HttpServletRequest request){
			return Global.ADMIN_PATH+"action/actionList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<ActionModel> save(ActionModel action,HttpServletRequest request){
		Result<ActionModel> json = new Result<>();
		ActionModel action2= actionService.selectByUrl(action.getActionUrl());
		boolean success = true;
		if(action2 != null){
			json.setMessage("该地址已经存在了!");
			success = false;
		}else{
			success = actionService.insertSelective(action);
			if(success){
				json.setMessage("添加成功!");
			}else{
				json.setMessage("添加失败!");
			}
		}
		json.setSuccess(success);
		json.setObj(action);
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<String> json = new Result<>();
		boolean success = actionService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage("删除成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("删除失败!");
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(ActionModel action,HttpServletRequest request){
		Result<String> json = new Result<String>();
		boolean success = actionService.updateByPrimaryKeySelective(action);
		if(success){
			json.setMessage("修改成功!");
			json.setSuccess(true);
		}else{
			json.setMessage("修改失败!");
			json.setSuccess(false);
		}
		return json;
	}
}
